package com.xuexiang.xuidemo.slice;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.dialog.EasyPopupDialog;
import com.xuexiang.xuidemo.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

/**
 * EasyPop弹出窗
 */
public class EasyPopSlice extends AbilitySlice implements Component.ClickedListener {
    private Context mContext;
    private Text text_title,text_title_bottom;
    private Image image_back;
    private Button btn_circle_comment,btn_circle_comment_left,btn_circle_comment_right,btn_circle_comment_top,btn_circle_comment_bottom;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_easy_pop);
        inintComponent();
        initTitle();
        inintClickedListener();
    }

    private void inintComponent() {
        mContext=EasyPopSlice.this;
        image_back=(Image) findComponentById(ResourceTable.Id_image_back);
        text_title=(Text)findComponentById(ResourceTable.Id_text_title);
        text_title_bottom=(Text)findComponentById(ResourceTable.Id_text_title_bottom);
        btn_circle_comment=(Button) findComponentById(ResourceTable.Id_btn_circle_comment);
        btn_circle_comment_left=(Button) findComponentById(ResourceTable.Id_btn_circle_comment_left);
        btn_circle_comment_right=(Button)findComponentById(ResourceTable.Id_btn_circle_comment_right);
        btn_circle_comment_top=(Button)findComponentById(ResourceTable.Id_btn_circle_comment_top);
        btn_circle_comment_bottom=(Button)findComponentById(ResourceTable.Id_btn_circle_comment_bottom);
    }

    private void initTitle() {
        text_title.setText("EasyPopup");
        text_title_bottom.setText("可自定义的弹出窗");
    }

    private void inintClickedListener() {
        image_back.setClickedListener(this);
        btn_circle_comment_left.setClickedListener(this);
        btn_circle_comment_right.setClickedListener(this);
        btn_circle_comment_top.setClickedListener(this);
        btn_circle_comment_bottom.setClickedListener(this);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_image_back:
                    onBackPressed();
                    break;
            case ResourceTable.Id_btn_circle_comment_left:
                final EasyPopupDialog lefteasyPopupDialog=new EasyPopupDialog(mContext,null);
                lefteasyPopupDialog.setSize(340,90);
                int location[] =btn_circle_comment.getLocationOnScreen();
                int x=location[0];
                int y=location[1];
                lefteasyPopupDialog.setOffset(x-340,y-8);
                lefteasyPopupDialog.setAutoClosable(true);
                lefteasyPopupDialog.show();
                lefteasyPopupDialog.setBtnznaClickedListener(component17 -> {
                    ToastUtil.showToast(mContext,"点赞");
                    lefteasyPopupDialog.destroy();
                });
                lefteasyPopupDialog.setBtncomlClickedListener(component16 -> {
                    ToastUtil.showToast(mContext,"评论");
                    lefteasyPopupDialog.destroy();
                });
                    break;
            case ResourceTable.Id_btn_circle_comment_right:
                final  EasyPopupDialog righteasyPopupDialog=new EasyPopupDialog(mContext,null);
                righteasyPopupDialog.setSize(340,90);
                int locationright[] =btn_circle_comment.getLocationOnScreen();
                int rightx=locationright[0];
                int righty=locationright[1];
                righteasyPopupDialog.setOffset(rightx+(btn_circle_comment.getWidth()),righty-8);
                righteasyPopupDialog.setAutoClosable(true);
                righteasyPopupDialog.show();
                righteasyPopupDialog.setBtnznaClickedListener(component15 -> {
                    ToastUtil.showToast(mContext,"点赞");
                    righteasyPopupDialog.destroy();
                });
                righteasyPopupDialog.setBtncomlClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ToastUtil.showToast(mContext,"评论");
                        righteasyPopupDialog.destroy();
                    }
                });
                    break;
            case ResourceTable.Id_btn_circle_comment_top:
                final  EasyPopupDialog easyPopupDialog=new EasyPopupDialog(mContext,btn_circle_comment);
                easyPopupDialog.setMode(LayoutAlignment.TOP);
                easyPopupDialog.setAutoClosable(true);
                easyPopupDialog.show();
                easyPopupDialog.setBtnznaClickedListener(component14 -> {
                    ToastUtil.showToast(mContext,"点赞");
                    easyPopupDialog.destroy();
                });
                easyPopupDialog.setBtncomlClickedListener(component13 -> {
                    ToastUtil.showToast(mContext,"评论");
                    easyPopupDialog.destroy();
                });
                    break;
            case ResourceTable.Id_btn_circle_comment_bottom:
              final   EasyPopupDialog easyPopupDialog2=new EasyPopupDialog(mContext,btn_circle_comment);
                easyPopupDialog2.setMode(LayoutAlignment.BOTTOM);
                easyPopupDialog2.setAutoClosable(true);
                easyPopupDialog2.show();
                easyPopupDialog2.setBtnznaClickedListener(component1 -> { ToastUtil.showToast(mContext,"点赞");easyPopupDialog2.destroy();});
                easyPopupDialog2.setBtncomlClickedListener(component12 -> { ToastUtil.showToast(mContext,"评论");easyPopupDialog2.destroy();});
                    break;
            default:
                throw new IllegalStateException("Unexpected value: " + component.getId());
        }
    }
}
